package cn.kgc.controller;

import cn.kgc.entities.ItemCategory;
import cn.kgc.service.ItemCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Controller
@Slf4j
public class ItemCategoryController {
    @Autowired
    private ItemCategoryService itemCategoryService;
    @GetMapping("/itemCategory")
    public String getAllItemCategory(Model model){
        List<ItemCategory> ics = itemCategoryService.getAllItemCategory();
        model.addAttribute("ics",ics);
        log.debug("debug:list"+ics);
        return "itemCategoryList";
    }
    @GetMapping("/itemCategory/view")
    public String toAddView(Model model){
        model.addAttribute("itemCategory",new ItemCategory());
        return "itemCategory";
    }

    @PostMapping("/itemCategory")
    public String addItemCategory(@Valid ItemCategory ic, BindingResult result){
        if(result.getErrorCount()>0){
            return "itemCategory";
        }
        itemCategoryService.addItemCategory(ic);
        return "redirect:/itemCategory";
    }
    @GetMapping("/itemCategory/{id}")
    public String getItemCategoryById(@PathVariable("id") Integer id, Model model){
        ItemCategory ic = itemCategoryService.getItemCategoryById(id);
        model.addAttribute("itemCategory",ic);
        return "itemCategory";
    }
    @PutMapping("/itemCategory")
    public String updateItemCategory(ItemCategory ic){
        itemCategoryService.updateItemCategory(ic);
        return "redirect:/itemCategory";
    }
    @DeleteMapping("/itemCategory/{id}")
    public String a(@PathVariable("id") Integer id){
        itemCategoryService.deleteItemCategoryById(id);
        return "redirect:/itemCategory";
    }
}
